#!-*- encoding:utf8 -*-
from django.template import Template, Context, RequestContext
from django.shortcuts import render_to_response
from django.contrib.auth.models import User
from django.contrib.auth.decorators import user_passes_test, login_required
from django.http import Http404, HttpResponseRedirect, HttpResponse
from django.core.paginator import Paginator,InvalidPage,EmptyPage,PageNotAnInteger

from osmp.accident.models import Accident
from osmp.host.models import Product

from django.db.models import Q

import time

def aclist(request):
    alst = Accident.objects.all();
    plist = Product.objects.all()
    request.session['plist'] = plist
    return render_to_response("accident.html",locals())

def addac(request):
    if request.method == 'POST':
        errors = []
        messages = []
        #数据验证
        if not request.POST.get('description', ''):
            errors.append('请输入事故详情')
        if len(request.POST.get('description', '')) > 50:
            errors.append('监控名称过长，已经过超过50个字节')
        if not request.POST.get('reason', ''):
            errors.append('请输入事故原因')
        if not request.POST.get('process',''):
            errors.append('请输入解决过程')
        if not request.POST.get('experience',''):
            errors.append('请输入经验教训')
        if not request.POST.get('duty',''):
            errors.append('请输入责任人')
        if not request.POST.get('abegintime',''):
            errors.append('请选择开始的时间')
        if not request.POST.get('acpid',''):
            errors.append('请输入对应的产品线')
        if not request.POST.get('alevel',''):
            errors.append('请选择级别')
        if not request.POST.get('atype',''):
            errors.append('请选择类型')
        if errors:
            #验证未通过 - 返回错误提示信息
            return render_to_response("addaccident.html",locals())
        if not errors:
            #验证通过 - 进行数据持久化操作
            Accident.objects.create(
                description = request.POST.get('description'),
                reason = request.POST.get('reason'),
                process = request.POST.get('process'),
                experience = request.POST.get('experience'),
                duty = request.POST.get('duty'),
                abegintime = request.POST.get('abegintime'),
                acpid = Product.objects.get(id = request.POST.get('acpid', '')),
                alevel = request.POST.get('alevel'),
                atype = request.POST.get('atype'),
                time  = time.time()
            )
            messages.append('addsuccess')
            return render_to_response("accident.html",locals())
    else:
        plist = Product.objects.all()
        return render_to_response("addaccident.html",locals())

@login_required
def search(request):
    messages = []
    
    q = Q()
    
    description = request.POST.get('description', '')
    if description:
        q &= Q(description__contains = description)
    
    acpid = request.POST.get('acpid', '')
    if acpid:
        q &= Q(acpid=acpid)
        
    alevel = request.POST.get('alevel', '')
    if alevel:
        q &= Q(alevel = alevel)
        
    atype = request.POST.get('atype', '')
    if atype:
        q &= Q(atype=atype)
    print description,acpid,alevel,atype
    alst = Accident.objects.filter(q)
    if not alst:
        messages.append("null")
        
    plist = request.session.get('plist')
    return render_to_response("accident.html",locals())

def modify(request):
    if request.method == 'POST':
        errors = []
        messages = []
        #数据验证
        if not request.POST.get('description', ''):
            errors.append('请输入事故详情')
        if len(request.POST.get('description', '')) > 50:
            errors.append('监控名称过长，已经过超过50个字节')
        if not request.POST.get('reason', ''):
            errors.append('请输入事故原因')
        if not request.POST.get('process',''):
            errors.append('请输入解决过程')
        if not request.POST.get('experience',''):
            errors.append('请输入经验教训')
        if not request.POST.get('duty',''):
            errors.append('请输入责任人')
        if not request.POST.get('abegintime',''):
            errors.append('请选择开始的时间')
        if not request.POST.get('acpid',''):
            errors.append('请输入对应的产品线')
        if not request.POST.get('alevel',''):
            errors.append('请选择级别')
        if not request.POST.get('atype',''):
            errors.append('请选择类型')
        if errors:
            #验证未通过 - 返回错误提示信息
            id = request.POST.get('id','')
            print id,"----"
            plist = Product.objects.all()
            if id:
                alst = Accident.objects.get(id = id)
                id = alst.acpid.id
                name = alst.acpid.name
            return render_to_response("modifyaccident.html",locals())
        if not errors:
            #验证通过 - 进行数据持久化操作
            id = request.POST.get('id','')
            print "---%s"%id
            Accident.objects.filter(id=id).update(
                description = request.POST.get('description'),
                reason = request.POST.get('reason'),
                process = request.POST.get('process'),
                experience = request.POST.get('experience'),
                duty = request.POST.get('duty'),
                abegintime = request.POST.get('abegintime'),
                acpid = Product.objects.get(id = request.POST.get('acpid')),
                alevel = request.POST.get('alevel'),
                atype = request.POST.get('atype'),
                time  = time.time()
            )
            alst = Accident.objects.all();
            messages.append('ms')
            return render_to_response("accident.html",locals())
    else:
        id = request.GET.get('id','')
        plist = Product.objects.all()
        if id:
            alst = Accident.objects.get(id = id)
            id = alst.acpid.id
            name = alst.acpid.name
            return render_to_response( "modifyaccident.html",locals())

def info(request,id):
    ainfo = Accident.objects.get(id = id)
    return render_to_response("accinfo.html",locals())

def delete(request,id):
    ainfo = Accident.objects.get(id = id)
    ainfo.delete()
    alst = Accident.objects.all();
    return render_to_response("accident.html",locals())
